<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Random Integer Processor</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .container {
            border: 1px solid #ddd;
            padding: 20px;
            border-radius: 5px;
            background-color: #f9f9f9;
        }
        textarea {
            width: 100%;
            height: 200px;
            margin: 10px 0;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-family: monospace;
        }
        .button-group {
            display: flex;
            justify-content: space-between;
            margin-top: 15px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 10px;
            padding: 5px 8px;
            margin: 2px 5px;
            cursor: pointer;
            font-size: 14px;
        }
        button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>随机产生20个4位整数并从小到大进行排序</h1>

        <textarea id="output" readonly></textarea>

        <div class="button-group">
            <button id="generate">随机产生20个整数</button>
            <button id="filter">找出能被5整除的整数</button>
            <button id="reset">重置</button>
        </div>
    </div>

    <script>
        $(document).ready(function() {
            let numbers = [];

            // Generate 20 random 4-digit numbers
            $('#generate').click(function() {
                numbers = [];
                for (let i = 0; i < 20; i++) {
                    numbers.push(Math.floor(Math.random() * 9000) + 1000);
                }

                // Sort the numbers
                numbers.sort((a, b) => a - b);

                // Display the results
                let output = "随机产生20个4位整数，分别如下：\n";
                output += numbers.join(', ') + "\n\n";
                output += "从小到大排序后结果为：\n";
                output += numbers.join(', ');

                $('#output').val(output);
            });

            // Filter numbers divisible by 5
            $('#filter').click(function() {
                if (numbers.length === 0) {
                    alert('请先生成随机数！');
                    return;
                }

                const divisibleBy5 = numbers.filter(num => num % 5 === 0);

                let currentOutput = $('#output').val();
                if (divisibleBy5.length > 0) {
                    currentOutput += "\n\n能被5整除的整数有：\n";
                    currentOutput += divisibleBy5.join(', ');
                } else {
                    currentOutput += "\n\n没有能被5整除的整数";
                }

                $('#output').val(currentOutput);
            });

            // Reset the output
            $('#reset').click(function() {
                numbers = [];
                $('#output').val('');
            });
        });
    </script>
</body>
</html>